<template>
	<scroll-view class="top-tab-bar__container" :show-scrollbar="false" enhanced scroll-with-animation scroll-x :scroll-into-view="'tab-item-' + (_current - 1)">
		<view v-for="(item, index) in tabs" :id="'tab-item-' + index" class="top-tab-item" @click="onTabItemTap($event, index)">
			<text :class="['top-tab-item_name', {'top-tab-item_name__selected': index == _current}]">{{itemKey ? item[itemKey]:item}}</text>
		</view>
	</scroll-view>
</template>

<script>
	export default {
		name:"top-tab-bar",
		props: {
			itemKey: {
				type: String,
				default: ""
			},
			tabs: {
				type: Array,
				default: []
			},
			current: {
				type: Number,
				default: 0
			}
		},
		watch: {
			current(newVal, oldVal) {
				this.$nextTick(() => {
					this._current = newVal
				})
			},

			_current(newVal, oldVal) {
				this.$nextTick(() => {
					this.$emit("changed", {value: newVal})
				})
			}
		},
		data() {
			return {
				_current: this.current
			};
		},
		methods: {
			onTabItemTap: function(event, index) {
				this._current = index
			}
		}
	}
</script>

<style scoped>
	.top-tab-bar__container {
		white-space: nowrap;
		width: 100%;
		background: white;
	}
	
	.top-tab-item {
		display: inline-block;
		min-width: 33.3333%;
		height: 96rpx;
		padding: 0 20rpx;
		box-sizing: border-box;
		text-align: center;
		line-height: 96rpx;
	}
	
	.top-tab-item_name {
		display: inline-block;
		font-size: 30rpx;
		height: 100%;
		position: relative;
		color: #333333;
	}
	
	.top-tab-item_name__selected {
		color: #2982F0;
	}
	
	.top-tab-item_name__selected::after {
		content: "";
		position: absolute;
		bottom: 0;
		left: 0;
		right: 0;
		height: 6rpx;
		background: #2982F0;
		border-radius: 6rpx;
		box-sizing: border-box;
	}
</style>